﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;

namespace LEVNet
{
    namespace LEVNetSocket
    {
        public static class SocketCommonDefine
        {
            public const string PACKET_EOF_TOKEN = "<EOF>";
        }
        public enum SocketStatusType
        {
            ReConnectException,
            ConnectException,
            DisConnectException,
            AcceptException,
            ReceiveException,
            SendException,
        }
        public class SocketStatusEventArgs : EventArgs
        {
            public int Threshold { get; set; }
            public DateTime TimeReached { get; set; }
        }

        public enum LEVSocketEventType
        {
            ClientConnect,
            ClientDisconnect,
            ConnectToServer,
            DisconnectFromServer,
            ServerStart,
            ServerStop
        }
        public enum LEVSocketExceptionType
        {
            ReConnectException,
            ConnectException,
            DisConnectException,
            AcceptException,
            ReceiveException,
            SendException,
        }
        public enum SocketCmdToken : int
        {   //必需三個單字, 皆英文
            //第一個單字小寫
            //第二個單字之第一個字母大寫, 其他字母小寫
            //第三個單字大寫
            _packetDataSTART_,
            _packetDataEOF_,
            _netCmdSTART_,
            _netCmdEOF_,
            _netCmdSUCCESS_,
            _netBeDISCONNECT_
        }
        public enum SocketReceivingTyp
        {
            Fragment,
            WholePacket,
            OldVerWholePacket
        }

        // State object for reading client data asynchronously
        public class SocketStateObj
        {
            // Client  socket.
            public Socket workSocket = null;
            // Size of receive buffer.
            public const int BufferSize = 1024;
            // Receive buffer.
            public byte[] buffer = new byte[BufferSize];
            // Receive buffer for append.
            public List<byte> listBuffer = new List<byte>();

            // check if it old version packet
            //public bool isPreviousVerPacket = false;

            // Received data string. [for Old Version using]
            //public StringBuilder sb = new StringBuilder();
        }
        public static class LEVSocketUtilities
        {
            public static int ByteArrayIndexOf(byte[] source, string strMatch)
            {
                return ByteArrayIndexOf(source, Encoding.ASCII.GetBytes(strMatch));
            }
            public static int ByteArrayIndexOf(byte[] source, byte[] byteMatch)
            {
                if (source == null
                    || byteMatch == null
                    || source.Length == 0
                    || byteMatch.Length == 0
                    || byteMatch.Length > source.Length)
                {
                    return -1;
                }

                int maxindex = source.Length - byteMatch.Length + 1;
                for (int i = 0; i < maxindex; i++)
                {
                    bool match_flag = true;
                    for (int j = 0; j < byteMatch.Length; j++)
                    {
                        if (source[i + j] != byteMatch[j])
                        {
                            match_flag = false;
                            break;
                        }
                    }
                    //判定結果  如果是就回傳當前index
                    if (match_flag == true)
                    {
                        return i;
                    }
                }
                return -1;//找不到就回傳-1
            }
        }//public static class LEVSocketUtilities
    }
}
